package mo.daliy;

import java.util.HashMap;
import java.util.Map;

public class L3202 {

    public int maximumLength(int[] nums, int k) {
        int n = nums.length;
        int[][] dp = new int[n][k];
        Map<Integer, Integer> last = new HashMap<>();

        int ret = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < k; j++) {
                int r = (j - nums[i] % k + k) % k;
                if (last.containsKey(r)) {
                    dp[i][j] = dp[last.get(r)][j] + 1;
                    ret = Math.max(ret, dp[i][j]);
                } else {
                    dp[i][j] = 1;
                }
            }
            last.put(nums[i] % k, i);
        }

        return ret;
    }

}
